package com.mbc.controller;

import com.mbc.comment.Result;
//import com.mbc.config.JwtProperties;
import com.mbc.dto.UserLoginDTO;
import com.mbc.dto.UserRealNameDTO;
import com.mbc.pojo.MbcUser;
import com.mbc.pojo.MbcUserInfo;
import com.mbc.service.UserService;
import com.mbc.utils.JwtUtil;
import com.mbc.vo.MbcUserInfoVO;
import com.mbc.vo.PhoneVO;
import com.mbc.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


/**
 * 用户控制器
 *
 * @author suimeng
 * @date 2024/04/08
 */
@RestController
@RequestMapping("/user")
@Api(tags = "基本用户接口")
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /*@Autowired
    private JwtProperties jwtProperties;*/

    /**
     * 微信登录
     *
     * @param userLoginDTO 用户登录 DTO
     * @return {@link Result}<{@link UserLoginVO}>
     */
    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        //微信登录
        UserLoginVO userLoginVO = userService.wxLogin(userLoginDTO);
        return Result.success(userLoginVO);
    }
    /*public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {

        //微信登录
        MbcUser user = userService.wxLogin(userLoginDTO);

        //为用户生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("Id",user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .Id(user.getId())
                .openId(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }*/

    /**
     * 粉丝数量
     *
     * @param userId 用户 ID
     * @return {@link Result}<{@link Integer}>
     */
    @GetMapping("/fans/{userId}")
    @ApiOperation("用户粉丝数量")
    public Result<Integer> fansNumber(@PathVariable String userId) {
        int count = userService.getFansCount(userId);
        return Result.success(count);
    }

    /**
     * 关注数量
     *
     * @param userId 用户 ID
     * @return {@link Result}<{@link Integer}>
     */
    @GetMapping("/concern/{userId}")
    @ApiOperation("用户关注数量")
    public Result<Integer> concernNumber(@PathVariable String userId) {
        int count = userService.getConcernCount(userId);
        return Result.success(count);
    }

    /**
     * 更改or添加昵称
     *
     * @param userId 用户 ID
     * @return
     */
    @PostMapping("/nickname/{userId}")
    @ApiOperation("更改or添加昵称")
    public Result<Boolean> changeNicknames(@RequestParam String nickname, @PathVariable String userId) {
        return Result.success(userService.updateNicknames(userId, nickname));
    }

    /**
     * 修改or添加电话
     *
     * @param phoneNumber 电话号码
     * @param userId      用户 ID
     * @return {@link Result}<{@link PhoneVO}>
     */
    @PostMapping("/phone/{userId}")
    @ApiOperation("修改or添加电话号码")
    public Result<Boolean> checkandaddPhone(@RequestParam String phoneNumber, @PathVariable String userId) {
        return Result.success(userService.updateandaddPhone(userId, phoneNumber));
    }

    /**
     * 更新或添加真实姓名
     *
     * @param userId          用户 ID
     * @param userRealNameDTO 用户真实姓名 DTO
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/name/{userId}")
    @ApiOperation("更新or添加真实名字")
    public Result<Boolean> updateOrAddRealName(@PathVariable String userId,
                                               @RequestBody UserRealNameDTO userRealNameDTO) {
        return Result.success(userService.updateOrAddRealName(userId, userRealNameDTO));
    }

    /**
     * 更新或添加详细地址
     *
     * @param userId  用户 ID
     * @param address 加法器
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/address/{userId}")
    @ApiOperation("更新或添加详细地址")
    public Result<Boolean> updateOrAddFullAddress(@PathVariable String userId, @RequestParam String address) {
        return Result.success(userService.updateOrAddFullAddress(userId, address));
    }

    /**
     * 更新或添加头像
     *
     * @param userId         用户 ID
     * @param profilePicture 个人资料图片
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/headshot/{userId}")
    @ApiOperation("更新或添加头像")
    public Result<Boolean> updateOrAddHeadShot(@PathVariable String userId, @RequestParam String profilePicture) {
        return Result.success(userService.updateOrAddHeadShot(userId, profilePicture));
    }

    /**
     * 更新电子邮件
     *
     * @param userId 用户 ID
     * @param email  电子邮件
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/email/{userId}")
    @ApiOperation("更新邮箱")
    public Result<Boolean> updateEmail(@PathVariable String userId, @RequestParam String email) {
        return Result.success(userService.updateEmail(userId, email));
    }

    /**
     * 新增用户详细信息
     *
     * @param userId      用户 ID
     * @param mbcUserInfo MBC 用户信息
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/{userId}")
    @ApiOperation("新增用户详细信息")
    public Result<Boolean> insertUserInfo(@PathVariable String userId, @RequestBody MbcUserInfo mbcUserInfo) {
        return Result.success(userService.insertUserInfo(userId, mbcUserInfo));
    }

    /**
     * 更新出生日期
     *
     * @param userId      用户 ID
     * @param dateOfBirth 出生日期
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/birth/{userId}")
    @ApiOperation("更新出生日期")
    public Result<Boolean> updateDateOfBirth(@PathVariable String userId, @RequestParam String dateOfBirth) {
        return Result.success(userService.updateDateOfBirth(userId, dateOfBirth));
    }

    /**
     * 更新个人标签
     *
     * @param userId       用户 ID
     * @param personalTags 个人标签
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/personalTags/{userId}")
    @ApiOperation("更新或添加个人标签")
    public Result<Boolean> updatePersonalTags(@PathVariable String userId, @RequestParam String personalTags) {
        return Result.success(userService.updatePersonalTags(userId, personalTags));
    }

    /**
     * 更新或添加个人简介
     *
     * @param userId          用户 ID
     * @param personalProfile 个人简介
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/personalprofile/{userId}")
    @ApiOperation("更新或添加个人简介")
    public Result<Boolean> updatePersonalProfile(@PathVariable String userId, @RequestParam String personalProfile) {
        return Result.success(userService.updatePersonalProfile(userId, personalProfile));
    }

    /**
     * 查询用户详细信息
     *
     * @return {@link Result}<{@link MbcUserInfo}>
     */
    @GetMapping("/get/{userId}")
    @ApiOperation("查询用户详细信息")
    public Result<MbcUserInfoVO> getUserInfo(@PathVariable String userId) {
        MbcUserInfoVO mbcUserInfoVO = userService.getByUserInfo(userId);
        return Result.success(mbcUserInfoVO);
    }
}
